#include "Shared.h"

sampler2D unit0 : register( s0 );
sampler2D unit1 : register( s1 );
sampler2D unit2 : register( s2 );
sampler2D unit3 : register( s3 );

void vsFunction(in vsGeometryIn vertex, out vsGeometryOut result, out float2 causticAOut:TEXCOORD5, out float2 causticBOut:TEXCOORD6)
{
	result.position0 = mul(vertex.position0, World);
	result.positionSys = mul(result.position0, View);
	result.positionSys = mul(result.positionSys, Projection);
	
	result.normal0 = mul(vertex.normal0, (float3x3)World);
	result.color0 = 1.0f;
	result.uv0 = vertex.uv0;
	result.uv1 = vertex.uv1;
	causticAOut = vertex.position0.xz * 0.05 + float2(sin(Time), cos(Time));
	causticBOut = float2(sin(Time - 3.14),cos(Time - 3.14)) + vertex.position0.xz * 0.1;
}


void psFunction(in psGeometryIn pixel, in float2 causticAIn:TEXCOORD5, in float2 causticBIn:TEXCOORD6, out float4 color : COLOR0)
{
	float causticFactor = saturate( 1 - ((pixel.position0.y / 2.0f) + 2) ) * 0.5;
	float2 coord = (pixel.uv1 + Time* 0.05);
	
	float4 caustics	= saturate( 
	lerp( tex2D(unit3, causticAIn + tex2D(unit1, coord ).xz * 0.2), 
	tex2D(unit3, causticBIn + tex2D(unit1, coord ).xz * 0.2),
	(sin(Time * 40) + 1.85) * 0.25));
	
	color = (tex2D(unit0, pixel.uv1) * tex2D(unit1, pixel.uv0)) + caustics * causticFactor;
	
	color+= tex2D(unit1, pixel.uv0) * saturate( tex2D(unit1, pixel.uv0) - .4 );
}

technique t0
{
    pass p0
    {
		VertexShader = compile vs_2_0 vsFunction();
		PixelShader = compile ps_2_0 psFunction();
    }
}
